<?php
namespace backend\controllers;
use common\Session;
use Yii;
use backend\models\Productct;
use yii\web\Controller;
/**
 * Created by PhpStorm.
 * User: 张
 * Date: 2015/10/25
 * Time: 18:04
 * /文章分类页面
 */
class ProductctController extends Controller{
    public function init()
    {
        Session::getSession()==101 ? :$this->redirect(['/login']);
    }
    public function actionIndex(){
        $model=new Productct();
        $page= isset($_GET['page'])? $_GET['page'] :1;
        if(!isset($_GET['cid'])){
            $a=Productct::find()->where(['>','cid',0])->asArray()->one();
            $parent_cid=$a['cid'];
        }else{
            $parent_cid=$_GET['cid'];
        }
        //添加数据
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            $model->name=$_POST['name'];
            $model->save();
            $this->redirect(['productct/index']);
        }

        $articlectModel=$this->actionPage($page,12,$parent_cid);
        $cid=Productct::find()->where(['level'=>1])->asArray()->all();
        $info=array(
            'rating'=>'产品分类',
            'define'=>'批量操作时，请选择操作类型！'
        );
        return $this->render('index',[
                'info'=>$info,
                'model'=>$model,
                'cid'=>$cid,//主类列表
                'click'=>$parent_cid,//主类选中状态
                'articlectModel'=>$articlectModel,//二级分类数据
            ]
        );
    }
    public function actionAdd(){
        $model=new Productct();
        $model->setScenario('add');
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            $model->level=2;
            $model->save();
            $this->redirect(['productct/index']);
        }
    }
    public function actionUpdateone(){
        $model=new Productct();
        $model->setScenario('update');
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            $data=Productct::find()->where(['cid'=>$model->cid])->one();
            var_dump($data);
            $data->name=$model->name;
            if($data->save()){
                $this->redirect(['productct/index']);
            };
            echo 'NO';
        }
    }
    public function actionDeleteone(){
        if(isset($_GET['cid'])){
            $cid=$_GET['cid'];
            $parent_cid=$_GET['parent_cid'];
        }else{
            return;
        }
        $all=Productct::find()->where(['cid'=>$cid])->all();
        $all[0]->delete();
        $this->redirect(['/productct/index','cid'=>$parent_cid]);
    }
    public function actionUpdateall(){
        if(Yii::$app->request->isAjax){
            $data="(".implode(',',$_POST['data']).")";
            $status=$_POST['status'];
            $connection = Yii::$app->db;
            //判断是不是删除
            if($status==0){
                $connection->createCommand("DELETE FROM qiche_product_category WHERE cid IN $data")->execute();
                return 'OK';
            }
            $connection->createCommand("UPDATE qiche_product_category SET parent_cid=$status WHERE cid IN $data")->execute();
            return 'OK';
        }
    }
    private function actionPage($pages,$limits,$parent_cid){
        //判断page参数是否是负数或O等非法参数值
        if($pages<1){
            $page=1;
        }else{
            $page=$pages;
        }
        $count=Productct::find()->where(['parent_cid'=>$parent_cid])->count();
        if(!$count){
            return array(
                'page'=>1,//分几页
                'model'=>'',//当前分页的数据
                'backpage'=>1,//返回上一页
                'frontpage'=>1,//下一页
                'nowpage'=>1,//当前页
                'limits'=>$limits,//每页的数据条数
                'parent_cid'=>$parent_cid//分类
            );
        }
        $pag=ceil($count/$limits);
        $start=($page-1)*$limits;
        if($pages>$pag){
            $start=($pag-1)*$limits;
        }
        $sql="SELECT * FROM qiche_product_category WHERE parent_cid=$parent_cid ORDER BY cid DESC LIMIT $start,$limits";
        $articlectModel = Productct::findBySql($sql)->asArray()->all();
        /*$cidName=Articlect::find()->where(['cid'=>$parent_cid])->asArray()->one();
        foreach($articlectModel as $k=>$v){
            $articlectModel[$k]['parent_cid']=$cidName['name'];
        }*/
        if($page==1){
            $backpage=1;
        }else{
            $backpage=$page-1;
        }
        if($page==$pag){
            $frontpage=$pag;
        }else{
            $frontpage=$page+1;
        }
        return array(
            'page'=>$pag,//分几页
            'model'=>$articlectModel,//当前分页的数据
            'backpage'=>$backpage,//返回上一页
            'frontpage'=>$frontpage,//下一页
            'nowpage'=>$page,//当前页
            'limits'=>$limits,//每页的数据条数
            'parent_cid'=>$parent_cid//分类
        );
    }
}
?>
